package com.reggie.background.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.background.entity.Category;
import com.reggie.background.entity.Dish;
import com.reggie.background.entity.Setmeal;
import com.reggie.background.exception.CustomException;
import com.reggie.background.service.CategoryService;
import com.reggie.background.mapper.CategoryMapper;
import com.reggie.background.service.DishService;
import com.reggie.background.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 1
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2022-07-15 14:14:23
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;

    /**
     * 删除菜单分类或套餐分类
     * @param id
     * @return
     */
    @Override
    public boolean removeCategory(Long id) {
        //根据菜品分类id来查询菜品，如果菜品大于0就禁止删除掉菜品分类操作
        LambdaQueryWrapper<Dish> qwdish=new LambdaQueryWrapper<Dish>();
        qwdish.eq(Dish::getCategoryId,id);
        int count = dishService.count(qwdish);
        if (count>0) {
            throw new CustomException("该菜品分类中已存在菜品项，禁止删除操作！");
        }
        //根据套餐分类id来查询套餐，如果套餐大于0就禁止删除掉套餐分类操作
        LambdaQueryWrapper<Setmeal> qwSetmeal=new LambdaQueryWrapper<Setmeal>();
        qwSetmeal.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(qwSetmeal);
        if (count1>0){
            throw new CustomException("该套餐分类中已存在套餐项，禁止删除操作！");
        }
        boolean result = this.removeById(id);

        return result;
    }
}




